package com.jike.noobmoney.mvp.view.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.ShouZhiListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.ShouZhiEntity;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 收支明细的列表
 */
public class ShouZhiFragment extends BaseNewFragment {

    public static String Channels = "channels";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;

    private String type;
    private String userid;
    private int page = 1;
    private Call<ShouZhiEntity> shouZhi;
    private List<ShouZhiEntity.UsermoneyBean> mData = new ArrayList<>();
    private ShouZhiListAdapter adapter;


    @Override
    protected int layoutResID() {
        return R.layout.fragment_shou_zhi;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        initData();
    }

    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getString(ShouZhiFragment.Channels);
        }

        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        getData(true);

        //adapter
        adapter = new ShouZhiListAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getData(true);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            getData(false);
        });


    }

    /**
     * 获取数据
     */
    private void getData(boolean isRefresh) {
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        showProgressDialog();
        shouZhi = RetrofitHelper.getInstance().getShouZhi(userid, type, "" + page, "10");
        shouZhi.enqueue(new Callback<ShouZhiEntity>() {
            @Override
            public void onResponse(Call<ShouZhiEntity> call, Response<ShouZhiEntity> response) {
                if (response != null) {
                    ShouZhiEntity body = response.body();
                    if (body != null) {
                        if ("100".equals(body.getCode())) {
                            List<ShouZhiEntity.UsermoneyBean> usermoney = body.getUsermoney();
                            if (usermoney != null && usermoney.size() > 0) {
                                if (isRefresh) {
                                    mData.clear();
                                    mData.addAll(usermoney);
                                } else {
                                    mData.addAll(usermoney);
                                }
                            }
                            if (empty != null) {
                                if (mData.size() > 0) {
                                    empty.setVisibility(View.GONE);
                                } else {
                                    empty.setVisibility(View.VISIBLE);
                                }
                            }
                            adapter.refresh(mData);

                        } else {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                        }
                    }
                }

                if (refreshLayout != null) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<ShouZhiEntity> call, Throwable t) {
                Log.e("xuke", t.getMessage());
                ToastUtils.showShortToastSafe(t.getMessage());
                if ( refreshLayout != null) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                }
                dismissProgressDialog();
            }
        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (shouZhi != null) {
            shouZhi.cancel();
        }
    }


}
